package com.fdhwws.web.dao.impl;

import com.fdh.myjdbcutil.MyJdbcUtil;
import com.fdhwws.web.dao.ChatRoomDao;
import com.fdhwws.web.domain.ChatRoom;

import java.util.List;

public class ChatRoomDaoImpl implements ChatRoomDao {
    @Override
    public ChatRoom selectById(Integer id) {
        return MyJdbcUtil.getInstance()
                .selectSql_upgrade("SELECT * FROM chat_room WHERE id =?",
                        new Object[]{id},
                        ChatRoom.class);
    }

    @Override
    public List<ChatRoom> selectAll() {
        return MyJdbcUtil.getInstance()
                .selectAll_upgrade("SELECT * FROM chat_room", ChatRoom.class);
    }

    @Override
    public int insert(ChatRoom entity) {
        return MyJdbcUtil.getInstance()
                .insertSql_upgrade(entity);
    }

    @Override
    public int update(ChatRoom entity) {
        return MyJdbcUtil.getInstance()
                .updateSql_upgrade(entity);
    }

    @Override
    public int delete(Integer id) {
        return MyJdbcUtil.getInstance()
                .updateSql("DELETE FROM chat_room WHERE id =?", new Object[]{id});
    }

    @Override
    public ChatRoom selectChartRoomByName(String name) {
        return MyJdbcUtil.getInstance()
                .selectSql_upgrade("SELECT id FROM chat_room WHERE name = ?",
                        new Object[]{name},
                        ChatRoom.class);
    }

    @Override
    public List<ChatRoom> selectChatRoomsByUid(Integer uid) {
        return MyJdbcUtil.getInstance()
                .selectAll_upgrade("SELECT * FROM chat_room WHERE create_user_id = ?",
                        new Object[]{uid},
                        ChatRoom.class);
    }

    @Override
    public Boolean verifyChatRoom(Integer roomId, String password) {
        ChatRoom chatRooms = MyJdbcUtil.getInstance()
                .selectSql_upgrade("SELECT id FROM chat_room WHERE id = ? AND password = ? AND valid = 1",
                        ChatRoom.class);
        return chatRooms != null;
    }

    @Override
    public Integer updateByRoomIdAndUid(ChatRoom room, Integer uid) {
        return MyJdbcUtil.getInstance()
                .updateSql_upgrade(room,
                        "WHERE id = ? AND create_user_id = ?",
                        new Object[]{room.getId(), uid});
    }

    @Override
    public Integer updateRoomValidByUid(Integer uid, Boolean valid) {
        return MyJdbcUtil.getInstance().
                updateSql("UPDATE chat_room SET valid = ? WHERE create_user_id = ?",
                        new Object[]{valid, uid});
    }
}
